#!/bin/bash

set -o errexit
set -o xtrace

test_dir=$(realpath $(dirname $0))
. ${test_dir}/../functions
cluster="auto-tuning"

RAM_SIZE=1073741824 # 1Gi
RDS_MEM_INSTANCE=12582880

create_infra $namespace

spinup_pxc "$cluster" "${test_dir}/conf/${cluster}1.yml"

AUTO_INNODB_SIZE=$(run_mysql \
                        'SELECT @@innodb_buffer_pool_size;' \
                        "-h $cluster-pxc -uroot -proot_password")
AUTO_CONNECTIONS=$(run_mysql \
                        'SELECT @@max_connections;' \
                        "-h $cluster-pxc -uroot -proot_password")

if [[ ${AUTO_INNODB_SIZE} != $((${RAM_SIZE} * 75 / 100)) ]]; then
    echo "innodb_buffer_pool_size is set to ${AUTO_INNODB_SIZE}, which does not correlate with cr.pxc.limits.memory * 0.75"
    exit 1
fi

if [[ ${AUTO_CONNECTIONS} != $((${RAM_SIZE} / ${RDS_MEM_INSTANCE})) ]]; then
    echo "max_connections is set to ${AUTO_CONNECTIONS}, which does not correlate with cr.pxc.limits.memory / ${RDS_MEM_INSTANCE}"
    exit 1
fi

apply_config "${test_dir}/conf/${cluster}2.yml"
wait_cluster_consistency "$cluster" 3

AUTO_INNODB_SIZE=$(run_mysql \
                        'SELECT @@innodb_buffer_pool_size;' \
                        "-h $cluster-pxc -uroot -proot_password")
AUTO_CONNECTIONS=$(run_mysql \
                        'SELECT @@max_connections;' \
                        "-h $cluster-pxc -uroot -proot_password")

if [[ ${AUTO_INNODB_SIZE} != $(( ${RAM_SIZE} / 2 * 75 / 100)) ]]; then
    echo "innodb_buffer_pool_size is set to ${AUTO_INNODB_SIZE}, which does not correlate with cr.pxc.requests.memory * 0.75"
    exit 1
fi

if [[ ${AUTO_CONNECTIONS} != $(( ${RAM_SIZE} / 2 / ${RDS_MEM_INSTANCE})) ]]; then
    echo "max_connections is set to ${AUTO_CONNECTIONS}, which does not correlate with cr.pxc.requests.memory / ${RDS_MEM_INSTANCE}"
    exit 1
fi

apply_config "${test_dir}/conf/${cluster}3.yml"
wait_cluster_consistency "$cluster" 3

INNODB_SIZE=$(run_mysql \
                        'SELECT @@innodb_buffer_pool_size;' \
                        "-h $cluster-pxc -uroot -proot_password")
CONNECTIONS=$(run_mysql \
                        'SELECT @@max_connections;' \
                        "-h $cluster-pxc -uroot -proot_password")

if [[ ${INNODB_SIZE} != 805306368 ]]; then
    echo "innodb_buffer_pool_size ${AUTO_INNODB_SIZE} is not set to 805306368"
    exit 1
fi

if [[ ${CONNECTIONS} != 200 ]]; then
    echo "max_connections ${CONNECTIONS} is not set to 200"
    exit 1
fi

destroy $namespace
